<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../lib/axios.js"></script>
</head>

<body>
    <h1>注册</h1>
    用户名: <input type="text" class="user"><br>
    密 码: <input type="text" class="pwd"><br>
    手机号: <select name="" id="">
        <option value="">+86</option>
        <option value="">+88</option>
        <option value="">+20</option>
        <option value="">+11</option>
        <option value="">+37</option>
    </select>
    <input type="text" class="phone"><br>
    <input type="text" class="code"><button class="btn">获取验证码</button><br>
    <button class="reginbtn">注册</button><a href="./login.html">去登录</a>
    <script>
        let user = document.querySelector('.user');
        let pwd = document.querySelector('.pwd');
        let phone = document.querySelector('.phone');
        let code = document.querySelector('.code');
        let btn = document.querySelector('.btn');
        let reginbtn = document.querySelector('.reginbtn')
        let codes;
        // 定义正则
        let get = /^1[3-9]\d{9}$/
        // 给获取验证码添加点击事件
        btn.onclick = function (e) {
            // 阻止默认行为
            e.preventDefault()
            if (get.test(phone.value.trim())) {
                axios.get('/a').then((res) => {
                    console.log(res.data);
                    codes = res.data;
                    let n = 6;
                    let time = setInterval(() => {
                        btn.innerHTML = --n + '秒后重新获取'
                        btn.disabled = true;
                        if (n <= 0) {
                            clearInterval(time);
                            btn.innerHTML = '请重新获取'
                            btn.disabled = false;
                        }
                    },1000)
                })
            }
        }
        // 给注册添加点击事件
        reginbtn.onclick = function(e) {
            // 阻止默认行为
            e.preventDefault()
            axios.post('/regin',{user:user.value,pwd:pwd.value}).then((res)=>{
                    console.log(res.data);
            })            
        }
    </script>
</body>

</html>